package com.spzx.product.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.spzx.common.core.utils.bean.BeanUtils;
import com.spzx.product.domain.Category;
import com.spzx.product.mapper.CategoryMapper;
import com.spzx.product.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spzx.product.vo.CategoryVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品分类 服务实现类
 * </p>
 *
 * @author spzx
 * @since 2024-12-19
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    /**
     * 根据parentId查找子节点
     *
     * @param parentId
     * @return
     */
    @Override
    public List<Category> treeSelect(Long parentId) {

        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getParentId, parentId);
        // 查询子节点
        List<Category> categoryList = baseMapper.selectList(queryWrapper);

        // 判断是否有子节点 懒加载子节点 也叫延迟加载
        if (!CollectionUtils.isEmpty(categoryList)) {
            categoryList.forEach(category ->
                    category.setHasChildren(
                            baseMapper.selectCount(
                                    new LambdaQueryWrapper<Category>()
                                            .eq(Category::getParentId, category.getId())
                            ) > 0
                    )
            );
        }

        return categoryList;
    }

    /**
     * 获取一级分类
     *
     * @return
     */
    @Override
    public List<CategoryVo> getLevelOneCategory() {
        // 查询一级分类
        List<Category> categoryList = baseMapper.selectList(
                // new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0L)  查询一级分类的id为0L
                //TODO
                new LambdaQueryWrapper<Category>().eq(Category::getParentId, 0L)
        );
        // 封装成CategoryVo对象
        List<CategoryVo> categoryVoList = categoryList.stream().map(category -> {
            CategoryVo categoryVo = new CategoryVo();
            BeanUtils.copyProperties(category, categoryVo);
            return categoryVo;
        }).collect(Collectors.toList());

        return categoryVoList;
    }

    /**
     * 构建树形结构
     *
     * @return
     */
    @Override
    public List<CategoryVo> tree() {
        // 查询所有分类 (不加条件)
        List<Category> categoryList = baseMapper.selectList(null);
        // 封装成CategoryVo对象
        List<CategoryVo> categoryVoList =
                categoryList.stream().map(
                        category -> {
                            CategoryVo categoryVo = new CategoryVo();
                            BeanUtils.copyProperties(category, categoryVo);
                            return categoryVo;
                        }
                ).collect(Collectors.toList());

        // 构建树形结构
        return buildTree(categoryVoList);
    }

    /**
     * 构建树形结构
     *
     * @param categoryVoList
     * @return
     */
    private List<CategoryVo> buildTree(List<CategoryVo> categoryVoList) {
        // 创建树形结构
        List<CategoryVo> trees = new ArrayList<>();
        //遍历所有节点
        for (CategoryVo categoryVo : categoryVoList) {
            // 找到根节点
            if (categoryVo.getParentId().longValue() == 0L) {//longValue() 转成Long类型
                // 添加到树形结构
                categoryVo = findChildren(categoryVo, categoryVoList);
                trees.add(categoryVo);
            }
        }
        // 返回树形结构
        return trees;
    }

    /**
     * 递归查找子节点children
     *
     * @param categoryVo
     * @param categoryVoList
     * @return
     */
    private CategoryVo findChildren(CategoryVo categoryVo, List<CategoryVo> categoryVoList) {
        //遍历所有节点
        for (CategoryVo node : categoryVoList) {
            // 找到子节点
            if (node.getParentId().longValue() == categoryVo.getId()) {
                // 添加到父节点的children中 组装到categoryVo中父子关系
                categoryVo.getChildren().add(findChildren(node, categoryVoList));
            }
        }
        return categoryVo;
    }
}
